package org.hcf.api;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.hcf.api.base.Helper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;

import static org.hcf.api.base.Helper.PLUGIN;

@Slf4j
@Component
public class PushMsgToPlugin {

    @Scheduled(fixedRate = 200)
    void sendMsg() {
//        log.info("《《《《《《《《《《《《《《《  准备推送PLUGIN消息");
        long now = System.currentTimeMillis();
        ArrayList<Long> list = new ArrayList<>(Helper.pluginPushMsg.keySet());
        for (Long time : list) {
            if (now - time >= 1000) {
                Collection<String> datas = Helper.pluginPushMsg.get(time);
                if (datas != null && datas.size() > 0) {
                    for (String data : datas) {
                        try {
                            String result = HttpUtil.post(PLUGIN.getUrl(), data);
                            log.info("延时推送PLUGIN消息" + data + "。结果:" + result);
                        } catch (Throwable ex) {
                            log.error("延迟推送PLUGIN消息" + data + "异常", ex);
                        }
                    }
                }
                Helper.pluginPushMsg.remove(time);
            }
        }
//        log.info("推送PLUGIN消息结束 》》》》》》》》》》》》》》》");
    }
}
